package cn.zxh.csdn_maven.work.flow.login;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.log4j.Logger;


public abstract class AbstractFlowController implements IFlowController{

	private Logger logger=Logger.getLogger(AbstractFlowController.class);
	private IFlowController flow;
	private Map<String, Object> result=new HashMap<String, Object>();
	/**自由的抽象方法*/
	public abstract Map<String, Object> execute(HttpClient client,Map<String, Object> params) throws ClientProtocolException, IOException;
	public abstract String getBaseUrl();
	/**设置本节点的下一个节点*/
	public IFlowController setNextFlow(IFlowController flow){
		this.flow=flow;
		return flow;
	}
	public Map<String, Object> run(HttpClient client,Map<String, Object> params) throws ClientProtocolException, IOException{
		result.putAll(execute(client,params));;
		if(result!=null && result.get("msg")!=null&&"success".equals(result.get("msg"))){
			logger.info(getBaseUrl()+"页面已经成功获取cookie");
			if(flow==null){
				logger.info(this.getClass().getSimpleName()+"已经是最后的工作链了");
			}else {
				result.putAll(this.flow.run(client,params));
			}
		}else {
			logger.info(getBaseUrl()+"获取cookie失败or该页面没有cookie可以获取");
		}
		return result;
	}
}
